package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.WumingErrorCode;
import com.wuming.house.dao.BannerMapper;
import com.wuming.house.dao.HouseLayoutMapper;
import com.wuming.house.model.Banner;
import com.wuming.house.model.HouseLayout;
import com.wuming.house.service.HouseLayoutService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("houseLayoutService")
public class HouseLayoutServiceImpl extends BaseServiceImpl implements HouseLayoutService {

    @Autowired
    private HouseLayoutMapper houseLayoutMapper;

    @Override
    public void saveHouseLayout(HouseLayout houseLayout) throws BusinessException {
        houseLayoutMapper.insert(houseLayout);
    }

    @Override
    public void modifyHouseLayout(HouseLayout houseLayout) throws BusinessException {
        houseLayoutMapper.updateByPrimaryKey(houseLayout);
    }

    @Override
    public void removeHouseLayoutById(Long id) throws BusinessException {
        houseLayoutMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<HouseLayout> getHouseLayoutByHouseId(Long houseId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("houseId", houseId);
        List<HouseLayout> houseLayouts = houseLayoutMapper.selectByParams(params);
        return houseLayouts;
    }

    @Override
    public List<HouseLayout> getHouseLayoutByPage(Long houseId, String layoutName, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (null != houseId) {
            params.put("houseId", houseId);
        }
        if (StringUtils.isNotBlank(layoutName)) {
            params.put("layoutName", layoutName);
        }
        try {
            return (List<HouseLayout>) this.getPageList(HouseLayoutMapper.class, "selectByParams", params, pageIndex, pageSize);
        } catch (Exception e) {
            logger.error("分页查询楼盘户型图失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(Long houseId, String layoutName) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        if (null != houseId) {
            params.put("houseId", houseId);
        }
        if (StringUtils.isNotBlank(layoutName)) {
            params.put("layoutName", layoutName);
        }
        try {
            return this.getTotal(HouseLayoutMapper.class, "selectTotal", params);
        } catch (Exception e) {
            logger.error("分页查询楼盘户型图总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
